package com.zzyl.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.service.ResourceService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.MenuVo;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资源前端控制器
 */
@Slf4j
@Api(tags = "资源管理")
@RestController
@RequestMapping("/resource")
public class ResourceController {


    @Autowired
    ResourceService resourceService;

    @PostMapping("/list")
    @ApiOperation(value = "资源列表", notes = "资源列表")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    public ResponseResult<List<ResourceVo>> resourceList(@RequestBody ResourceDto resourceDto) {
        List<ResourceVo> resourceVoList = resourceService.findResourceList(resourceDto);
        return ResponseResult.success(resourceVoList);
    }

    /**
     * @param resourceDto 资源对象
     * @return
     *  资源树形
     */
    @PostMapping("/tree")
    @ApiOperation(value = "资源树形", notes = "资源树形")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.label"})
    public ResponseResult<TreeVo> resourceTreeVo(@RequestBody ResourceDto resourceDto) {
//        TreeVo treeVo = resourceService.resourceTreeVo(resourceDto);;
        return ResponseResult.success(resourceService.resourceTreeVo(resourceDto));
    }

    @PutMapping
    @ApiOperation(value = "资源添加", notes = "资源添加")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState"
            , "resourceDto.icon"
            , "resourceDto.parentResourceNo"
            , "resourceDto.requestPath"
            , "resourceDto.resourceName"
            , "resourceDto.resourceType"
            , "resourceDto.sortNo"})
    public ResponseResult<ResourceVo> createResource(@RequestBody ResourceDto resourceDto) {
        resourceService.createResource(resourceDto);
        return ResponseResult.success();
    }


//    根据资源列表、资源树形结构、资源添加完善资源修改、启用禁用、删除资源菜单
//    TODO 资源修改
    @PatchMapping
    @ApiOperation(value = "资源修改", notes = "资源修改")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState"
            , "resourceDto.icon"
            , "resourceDto.parentResourceNo"
            , "resourceDto.requestPath"
            , "resourceDto.resourceName"
            , "resourceDto.resourceType"
            , "resourceDto.sortNo"})
    public ResponseResult<ResourceVo> updateResource(@RequestBody ResourceDto resourceDto) {
        resourceService.updateById(resourceDto);
        return ResponseResult.success();
    }

//    TODO 启用禁用
    @ApiOperation(value = "启动禁用", notes = "控制菜单的启动和禁用")
    @PostMapping("/enable")
    public ResponseResult updateStatus(
            @ApiParam("禁用的菜单吗和父菜单码以及状态")
            @RequestBody ResourceDto resourceDto){
        resourceService.updateStatus(resourceDto);
        return ResponseResult.success();
    }
//    TODO 删除资源菜单
//    启用状态下不可删除
    @ApiOperation(value = "删除资源", notes = "根据id删除资源")
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable String id){
        resourceService.delete(id);
        return ResponseResult.success();
    }


    @GetMapping("/myButten")
    public ResponseResult myButten(){
        Long userId = UserThreadLocal.getUserId();
        List<MenuVo> resourceVos = resourceService.myButten(userId);
        return ResponseResult.success(resourceVos);
    }
//
//    @GetMapping("/menus")
//    public ResponseResult menus(){
//        TreeVo treeVo = resourceService.getMenus();
//        return  ResponseResult.success(treeVo);
//    }

//    @GetMapping("/menus")
//    @ApiOperation(value = "左侧菜单", notes = "左侧菜单")
//    public ResponseResult<List<MenuVo>> menus() {
//
//        List<MenuVo> menus = resourceService.menus();
//        return ResponseResult.success(menus);
//    }
}